SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


  
CREATE VIEW [dbo].[TDPAGO_CXPA] ("CO_EMPR", "CO_ENTI", "TI_DOCU_ENTI", "NU_DOCU_ENTI", "IM_DETA", "FE_OPER", "FA_TIPO_CAMB", "CO_MONE", "NU_CHEQ", "NU_ASTO")     
AS     
  SELECT T1.CO_EMPR CO_EMPR, T1.CO_ENTI CO_ENTI, T1.TI_DOCU_ENTI TI_DOCU_ENTI, T1.NU_DOCU_ENTI NU_DOCU_ENTI, T1.IM_DETA IM_DETA,    
 T2.FE_OPER FE_OPER, T2.FA_TIPO_CAMB FA_TIPO_CAMB, T3.CO_MONE CO_MONE,    
        T2.NU_CHEQ NU_CHEQ, T2.NU_COMP_BANC NU_ASTO    
FROM TDMOVI_BANC T1, TCMOVI_BANC T2 , TMCNTA_BANC T3, TTDOCU_CNTB    
WHERE T1.CO_EMPR = T2.CO_EMPR    
  AND T1.CO_BANC = T2.CO_BANC    
  AND T1.NU_CNTA_BANC  = T2.NU_CNTA_BANC    
  AND T1.AA_BNCO = T2.AA_BNCO    
  AND T1.MM_BNCO = T2.MM_BNCO    
  AND T1.TI_MOVI_BANC = T2.TI_MOVI_BANC    
  AND T1.NU_COMP_BANC = T2.NU_COMP_BANC    
  AND T1.ST_CIER_DOCU = 'S'    
  AND T1.CO_CNTA_EMPR IS NULL    
  AND T2.TI_SITU = 'ACT'    
  AND T2.CO_EMPR = T3.CO_EMPR    
  AND T2.CO_BANC = T3.CO_BANC    
  AND T2.NU_CNTA_BANC = T3.NU_CNTA_BANC    
  AND T1.TI_DOCU_ENTI = TTDOCU_CNTB.TI_DOCU    
  AND TTDOCU_CNTB.ST_CXPA   =  'S'    
UNION ALL    
SELECT T1.CO_EMPR, T1.CO_ENTI, T1.TI_DOCU_ENTI, T1.NU_DOCU_ENTI, T1.IM_DETA ,    
  T2.FE_OPER, T2.FA_TIPO_CAMB , T3.CO_MONE,    
                Null, T2.NU_COMP_CAJA    
FROM TDMOVI_CAJA T1, TCMOVI_CAJA T2, TMCAJA T3, TTDOCU_CNTB    
WHERE T1.CO_EMPR = T2.CO_EMPR    
  AND T1.CO_CAJA = T2.CO_CAJA    
  AND T1.AA_CAJA = T2.AA_CAJA    
  AND T1.MM_CAJA = T2.MM_CAJA    
  AND T1.TI_MOVI = T2.TI_MOVI    
  AND T1.NU_COMP_CAJA = T2.NU_COMP_CAJA    
  AND T1.ST_CIER_DOCU = 'S'    
  AND T1.CO_CNTA_EMPR IS NULL    
  AND T2.TI_SITU = 'ACT'    
  AND T2.CO_EMPR = T3.CO_EMPR    
  AND T2.CO_CAJA = T3.CO_CAJA    
  AND T1.TI_DOCU_ENTI = TTDOCU_CNTB.TI_DOCU    
  AND TTDOCU_CNTB.ST_CXPA   =  'S'    
UNION ALL    
SELECT TDREND_GAST.CO_EMPR, TDREND_GAST.CO_ENTI, TDREND_GAST.CO_TIPO_DOCU, TDREND_GAST.NU_DOCU_PROV,    
  TDREND_GAST.IM_DOCU, TCREND_GAST.FE_REND_GAST, TDREND_GAST.FA_CAMB_DOCU, TMDOCP_GENE.CO_MONE,    
                Null, TCREND_GAST.NU_REND_GAST    
FROM TDREND_GAST, TCREND_GAST, TMDOCP_GENE, TTDOCU_CNTB    
WHERE TCREND_GAST.TI_SITU = 'ACT'    
  AND TDREND_GAST.CO_EMPR = TCREND_GAST.CO_EMPR    
  AND TDREND_GAST.CO_UNID_CONC = TCREND_GAST.CO_UNID_CONC    
  AND TDREND_GAST.FE_REND_GAST = TCREND_GAST.FE_REND_GAST    
  AND TDREND_GAST.NU_REND_GAST = TCREND_GAST.NU_REND_GAST    
  AND TDREND_GAST.CO_EMPR = TMDOCP_GENE.CO_EMPR    
  AND TDREND_GAST.CO_ENTI = TMDOCP_GENE.CO_PROV    
  AND TDREND_GAST.CO_TIPO_DOCU = TMDOCP_GENE.CO_TIPO_DOCU    
  AND TDREND_GAST.NU_DOCU_PROV = TMDOCP_GENE.NU_DOCU_PROV    
  AND TMDOCP_GENE.CO_TIPO_DOCU = TTDOCU_CNTB.TI_DOCU    
  AND TTDOCU_CNTB.ST_CXPA   =  'S'


GO
